package com.czh.dao.Provider;

import com.czh.dao.BaseDao;
import com.czh.pojo.Provider;
import com.mysql.cj.util.StringUtils;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProviderDaoImpl implements ProviderDao{

    @Override
    public List<Provider> providerList(Connection connection, String proCode, String proName) {
        PreparedStatement pre = null;
        ResultSet res = null;
        StringBuffer sql = new StringBuffer("select * from smbms_provider where 1=1 ");
        List<Provider> providerList = new ArrayList<>();
        if(connection!=null){
            List<Object> list = new ArrayList<>();
            if(!StringUtils.isNullOrEmpty(proCode)){
                sql.append(" and proCode like ? ");
                list.add("%"+proCode+"%");
            }
            if (!StringUtils.isNullOrEmpty(proName)){
                sql.append(" and proName like ? ");
                list.add("%"+proName+"%");
            }
            Provider provider;
            try {
                res = BaseDao.getSelect(connection, pre, res, sql.toString(), list.toArray());
                while (res.next()){
                    provider  = new Provider();
                    provider.setId(res.getInt("id"));
                    provider.setProCode(res.getString("proCode"));
                    provider.setProName(res.getString("proName"));
                    provider.setProDesc(res.getString("proDesc"));
                    provider.setProContact(res.getString("proContact"));
                    provider.setProPhone(res.getString("proPhone"));
                    provider.setProAddress(res.getString("proAddress"));
                    provider.setProFax(res.getString("proFax"));
                    provider.setCreationDate(res.getTimestamp("creationDate"));
                    providerList.add(provider);
                }
                BaseDao.closeConnect(null,pre,res);
            }catch (Exception e){
                throw new RuntimeException("Dao层providerList错误！");
            }
        }
        return providerList;
    }
}
